package cn.ibobo.lease.admin.controller;

import cn.ibobo.lease.admin.service.impl.ResLeaseServiceImpl;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.ListCopyToVOUtil;
import cn.ibobo.lease.common.util.PaginationVOUtil;
import cn.ibobo.lease.common.vo.ResultVO;
import cn.ibobo.lease.common.util.ResultVOUtil;
import cn.ibobo.lease.common.dto.ResLeaseDTO;
import cn.ibobo.lease.common.vo.ResLeaseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description 物品租赁表控制器
 * @Author lililiang
 * @Date 2018-12-13 18:36:40
 */

@RestController
@Api(tags = "物品租赁记录控制器")
@RequestMapping("resLease")
public class ResLeaseController{

    private static final Log LOGGER = LogFactory.getLog(ResLeaseController.class);

    @Autowired
    private ResLeaseServiceImpl service;
    @Autowired
    private ResultVOUtil<Object> resultObject;

    /**
	 * 物品租赁表列表
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
	 * @Param
	 * @Return
	 */
    @ApiOperation(value = "物品租赁表列表", response = ResLeaseVO.class)
    @PostMapping("list4Page")
    public ResultVO<Object> list4Page(
            ResLeaseVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ){
        try {
            ResLeaseDTO entityDTO = new ResLeaseDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<ResLeaseDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), PaginationVOUtil.getPaginationVO(list, ResLeaseVO.class, pageNum));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 物品租赁表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @ApiOperation(value = "物品租赁表列表（无分页）", response = ResLeaseVO.class)
    @PostMapping("listByCondition")
    public ResultVO<Object> listByCondition(ResLeaseVO entity){
        try {
            ResLeaseDTO entityDTO = new ResLeaseDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<ResLeaseDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, ResLeaseVO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 物品租赁表详细信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
//    @ApiOperation(value = "物品租赁表详细信息", response = ResLeaseVO.class)
//    @PostMapping("queryOneByPk")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") String id){
        try {
            ResLeaseDTO entityDTO = service.queryOneByPk(id);
            ResLeaseVO entity = new ResLeaseVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存物品租赁表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
//    @ApiOperation(value = "保存物品租赁表信息", response = ResultVO.class)
//    @PostMapping("save")
    public ResultVO<Object> save(ResLeaseVO entity) {
        try {
            ResLeaseDTO entityDTO = new ResLeaseDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            if(service.save(entityDTO) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 确认租用
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @ApiOperation(value = "确认租用", response = ResultVO.class)
    @PostMapping("lease")
    public ResultVO<Object> lease(
            @ApiParam(value = "租赁记录id",required = true) @RequestParam(value = "id") String id,
            @ApiParam(value = "租赁点人员",required = true) @RequestParam(value = "aid") String aid
    ) {
        try {
            ResLeaseDTO entityDTO = new ResLeaseDTO();
            entityDTO.setId(id);
            entityDTO.setAid(aid);
            Integer lease = service.lease(entityDTO);
            return resultObject.success(lease, ResultEnum.getMsg(lease));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 查询租赁费用（归还前使用）
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @ApiOperation(value = "查询租赁费用（归还前使用）", response = ResultVO.class)
    @PostMapping("queryCost")
    public ResultVO<Object> queryCost(
            @ApiParam(value = "租赁记录id",required = true) @RequestParam(value = "id") String id
    ) {
        try {
            ResLeaseDTO entityDTO = service.queryCost(id);
            ResLeaseVO entity = new ResLeaseVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 归还物品
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @ApiOperation(value = "归还物品", response = ResultVO.class)
    @PostMapping("back")
    public ResultVO<Object> back(
            @ApiParam(value = "租赁记录id",required = true) @RequestParam(value = "id") String id,
            @ApiParam(value = "租赁点人员",required = true) @RequestParam(value = "backaid") String backaid,
            @ApiParam(value = "租赁点id",required = true) @RequestParam(value = "backbid") String backbid,
            @ApiParam(value = "其他费用",required = true) @RequestParam(value = "other") BigDecimal other,
            @ApiParam(value = "租赁费用",required = true) @RequestParam(value = "leacost") BigDecimal leacost,
            @ApiParam(value = "总费用",required = true) @RequestParam(value = "totalcost") BigDecimal totalcost,
            @ApiParam(value = "备注",required = false) @RequestParam(value = "remark",required = false) String remark
    ) {
        try {
            ResLeaseDTO entityDTO = new ResLeaseDTO();
            entityDTO.setId(id);
            entityDTO.setBackaid(backaid);
            entityDTO.setBackbid(backbid);
            entityDTO.setOther(other);
            entityDTO.setLeacost(leacost);
            entityDTO.setTotalcost(totalcost);
            entityDTO.setRemark(remark);
            Integer back = service.back(entityDTO);
            return resultObject.success(back, ResultEnum.getMsg(back));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 删除物品租赁表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
//    @ApiOperation(value = "删除物品租赁表信息", response = ResultVO.class)
//    @PostMapping("delete")
    public ResultVO<Object> delete(@ApiParam(value = "id",required = true) @RequestParam(value = "id") String id){
        try {
            if(service.delete(id) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

}